package com.store.app.activity;

import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TimePicker;

import com.store.app.BaseActivity;
import com.store.app.R;
import com.store.app.net.NetRequest;
import com.store.app.net.ifc.IOnHttpListener;

import org.json.JSONObject;



/**
 * 特卖--配送
 */
public class DeliveryActivity extends BaseActivity implements View.OnClickListener, IOnHttpListener {

    private ImageView public_ll_return;
    private TextView tvTitle;

    private EditText et_delivery_area;//配送范围
    private EditText et_min_sum;//起送金额
    private TextView tv_start_time;//营业开始时间
    private TextView tv_end_time;//营业结束时间
    private NetRequest netRequest;
    private String deliveried_range;  //配送范围
    private String opening_time; //营业时间
    private String over_amount; //配送金额
    private LinearLayout ll_time;//营业时间控件
    private Button bt_submit;//提交
    private Dialog alterDialog;//选择时间范围弹窗
    private String startTime, endTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        initListener();
        initData();
    }

    private void initView() {
        setContentView(R.layout.activity_delivery);
        public_ll_return = (ImageView) findViewById(R.id.public_ll_return);
        tvTitle = (TextView) findViewById(R.id.tvTitle);
        et_delivery_area = (EditText) findViewById(R.id.et_delivery_area);//配送范围
        et_min_sum = (EditText) findViewById(R.id.et_min_sum);//起送金额
        tv_start_time = (TextView) findViewById(R.id.tv_start_time);//营业开始时间
        tv_end_time = (TextView) findViewById(R.id.tv_end_time);//营业结束时间
        ll_time = (LinearLayout) findViewById(R.id.ll_time);
        bt_submit = (Button) findViewById(R.id.bt_submit);
    }

    private void initListener() {
        public_ll_return.setOnClickListener(this);
        ll_time.setOnClickListener(this);
        bt_submit.setOnClickListener(this);
    }

    private void initData() {
        initTitleBar();
        //获取掌上便利店时间范围金额
        netRequest = new NetRequest(this);
        netRequest.requestBusinessInfoFind(1);
    }

    private void initTitleBar() {
        tvTitle.setText("配送");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_time:
                creatDialog();
                break;
            case R.id.public_ll_return:
                finish();
                break;
            case R.id.bt_submit:

                netRequest.requestSetStoresInfo(2, tv_start_time.getText().toString().trim() + "-" + tv_end_time.getText().toString().trim(), et_delivery_area.getText().toString().trim(), et_min_sum.getText().toString().trim());

                break;
        }
    }

    @Override
    public void onAfinalFail() {
        showToast("请检查本地网络!");
    }

    @Override
    public void onExecuteSuccess(int requestId, String result, String msg) {
        if (requestId == 1) {
            try {
                JSONObject jsonObject = new JSONObject(result);
                deliveried_range = jsonObject.getString("deliveried_range");
                opening_time = jsonObject.getString("opening_time");
                over_amount = jsonObject.getString("over_amount");
                if (TextUtils.isEmpty(deliveried_range)) {
                    et_delivery_area.setText("0");
                } else {
                    et_delivery_area.setText(deliveried_range);
                }
                et_delivery_area.setSelection(et_delivery_area.getText().toString().length());
                if (!TextUtils.isEmpty(opening_time) && opening_time.contains("-")) {
                    String[] arr = opening_time.split("-");
                    if (arr.length == 2) {
                        if (TextUtils.isEmpty(arr[0]) || arr[0].equals("null")) {
                            tv_start_time.setText("00:00");
                        } else {
                            tv_start_time.setText(arr[0]);
                        }
                        if (TextUtils.isEmpty(arr[1]) || arr[1].equals("null")) {
                            tv_end_time.setText("24:00");
                        } else {
                            tv_end_time.setText(arr[1]);
                        }
                    }
                }

                if (TextUtils.isEmpty(over_amount)) {
                    et_min_sum.setText("0.00");
                } else {
                    et_min_sum.setText(over_amount);
                }
                et_min_sum.setSelection(et_min_sum.getText().toString().length());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else if(requestId == 2){
            showToast("提交成功");
            finish();
        }
    }

    @Override
    public void onExecuteFail(int requestId, String msg) {
        showToast(msg);
    }

    private void creatDialog() {
        LinearLayout linearLayout = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.time_dialog, null);
        TextView tvCancel = (TextView) linearLayout.findViewById(R.id.cancel);
        TextView tvSubimt = (TextView) linearLayout.findViewById(R.id.submit);
        final TimePicker tp_start = (TimePicker) linearLayout.findViewById(R.id.tp_start);
        final TimePicker tp_end = (TimePicker) linearLayout.findViewById(R.id.tp_end);
        tp_start.setIs24HourView(true);
        tp_end.setIs24HourView(true);
        tp_start.setCurrentHour(0);
        tp_start.setCurrentMinute(0);
        tp_end.setCurrentHour(23);
        tp_end.setCurrentMinute(59);
        AlertDialog.Builder builder = new AlertDialog.Builder(DeliveryActivity.this);
        builder.setView(linearLayout);
        alterDialog = builder.show();
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alterDialog.dismiss();
            }
        });
        tvSubimt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (tp_start.getCurrentHour() < 10) {
                    startTime = "0" + tp_start.getCurrentHour();
                    if (tp_start.getCurrentMinute() < 10) {
                        startTime += ":0" + tp_start.getCurrentMinute();
                    } else {
                        startTime += ":" + tp_start.getCurrentMinute();
                    }
                } else {
                    startTime = "" + tp_start.getCurrentHour();
                    if (tp_start.getCurrentMinute() < 10) {
                        startTime += ":0" + tp_start.getCurrentMinute();
                    } else {
                        startTime += ":" + tp_start.getCurrentMinute();
                    }
                }

                if (tp_end.getCurrentHour() < 10) {
                    endTime = "0" + tp_end.getCurrentHour();
                    if (tp_end.getCurrentMinute() < 10) {
                        endTime += ":0" + tp_end.getCurrentMinute();
                    } else {
                        endTime += ":" + tp_end.getCurrentMinute();
                    }
                } else {
                    endTime = "" + tp_end.getCurrentHour();
                    if (tp_end.getCurrentMinute() < 10) {
                        endTime += ":0" + tp_end.getCurrentMinute();
                    } else {
                        endTime += ":" + tp_end.getCurrentMinute();
                    }
                }
                if (tp_end.getCurrentHour() > tp_start.getCurrentHour()) {
                    tv_start_time.setText(startTime);
                    tv_end_time.setText(endTime);
                    alterDialog.dismiss();
                } else {
                    showToast("请选择合理的营业时间!");
                }
                Log.v("zyl", "startTime:" + startTime);
                Log.v("zyl", "endTime:" + endTime);
                Log.v("zyl", "起始时间：" + tp_start.getCurrentHour() + ":" + tp_start.getCurrentMinute());
                Log.v("zyl", "结束时间：" + tp_end.getCurrentHour() + ":" + tp_end.getCurrentMinute());
            }
        });
    }
}
